/*
 * FiltryDialog.java
 *
 * Created on 18. prosinec 2006, 14:27
 */

package cz.results.winter6.view.dialogs.filtrdialog;

import cz.results.winter6.model.Filtr;
import cz.results.winter6.model.Filtry;
import javax.swing.JDialog;

public class FiltryDialog extends JDialog {
    
    
    private Filtry filtry;
    private Filtr filtr;
    private FiltryTableModel tm;
    
    public FiltryDialog(JDialog parent, boolean modal, Filtry filtry) {
        super(parent, modal);
        
        setFiltry(filtry);
        setTm(new FiltryTableModel(filtry));
        initComponents();
    }
    
    private void upravaFiltru(){
        final JDialog parent = this;
        
        if(jTable1.getSelectedRow()!=-1) {                            //musi byt vybran nejaky radek
            filtr = getFiltry().get(jTable1.getSelectedRow());
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new FiltrDialog(parent, true, filtry, filtr, false).setVisible(true);
                    getTm().fireTableDataChanged();
                }
            });
        }
    }
    
    
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jButtonNovy = new javax.swing.JButton();
        jButtonUprav = new javax.swing.JButton();
        jButtonSmaz = new javax.swing.JButton();
        jButtonMoveUp = new javax.swing.JButton();
        jButtonMoveDown = new javax.swing.JButton();
        jButtonZrus = new javax.swing.JButton();

        setTitle("konfigurace filtr\u016f");
        setLocationByPlatform(true);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                closeDialog(evt);
            }
        });

        jTable1.setModel(tm);
        jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTable1MouseClicked(evt);
            }
        });

        jScrollPane1.setViewportView(jTable1);

        jButtonNovy.setIcon(new javax.swing.ImageIcon(getClass().getResource("/cz/results/winter6/view/icons/edit_add.png")));
        jButtonNovy.setText("nov\u00fd");
        jButtonNovy.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonNovyActionPerformed(evt);
            }
        });

        jButtonUprav.setIcon(new javax.swing.ImageIcon(getClass().getResource("/cz/results/winter6/view/icons/edit.png")));
        jButtonUprav.setText("uprav");
        jButtonUprav.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonUpravActionPerformed(evt);
            }
        });

        jButtonSmaz.setIcon(new javax.swing.ImageIcon(getClass().getResource("/cz/results/winter6/view/icons/edit_remove.png")));
        jButtonSmaz.setText("sma\u017e");
        jButtonSmaz.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonSmazActionPerformed(evt);
            }
        });

        jButtonMoveUp.setIcon(new javax.swing.ImageIcon(getClass().getResource("/cz/results/winter6/view/icons/1uparrow.png")));
        jButtonMoveUp.setText("up");
        jButtonMoveUp.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonMoveUpActionPerformed(evt);
            }
        });

        jButtonMoveDown.setIcon(new javax.swing.ImageIcon(getClass().getResource("/cz/results/winter6/view/icons/1downarrow.png")));
        jButtonMoveDown.setText("down");
        jButtonMoveDown.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonMoveDownActionPerformed(evt);
            }
        });

        jButtonZrus.setIcon(new javax.swing.ImageIcon(getClass().getResource("/cz/results/winter6/view/icons/button_cancel.png")));
        jButtonZrus.setText("zru\u0161");
        jButtonZrus.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonZrusActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 688, Short.MAX_VALUE)
                    .add(layout.createSequentialGroup()
                        .add(jButtonNovy)
                        .add(16, 16, 16)
                        .add(jButtonUprav)
                        .add(14, 14, 14)
                        .add(jButtonSmaz)
                        .add(101, 101, 101)
                        .add(jButtonMoveUp, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 76, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(15, 15, 15)
                        .add(jButtonMoveDown)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .add(jButtonZrus)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 369, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 11, Short.MAX_VALUE)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jButtonNovy)
                    .add(jButtonSmaz)
                    .add(jButtonUprav)
                    .add(jButtonMoveUp)
                    .add(jButtonMoveDown)
                    .add(jButtonZrus))
                .addContainerGap())
        );
        pack();
    }// </editor-fold>//GEN-END:initComponents
    
    private void jButtonZrusActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonZrusActionPerformed
        setVisible(false);
        dispose();
    }//GEN-LAST:event_jButtonZrusActionPerformed
    
    private void jButtonMoveDownActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonMoveDownActionPerformed
        int selectedRow = jTable1.getSelectedRow();
        
        if(selectedRow > -1 && selectedRow < getTm().getRowCount()-1) {                            //musi byt vybran nejaky radek
            filtr = getFiltry().get(selectedRow);
            getFiltry().remove(filtr);
            getFiltry().add(selectedRow+1,filtr);
            getTm().fireTableDataChanged();
            jTable1.setRowSelectionInterval(selectedRow+1, selectedRow+1);
        }
    }//GEN-LAST:event_jButtonMoveDownActionPerformed
    
    private void jButtonMoveUpActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonMoveUpActionPerformed
        int selectedRow = jTable1.getSelectedRow();
        
        if(selectedRow > 0) {                            //musi byt vybran nejaky radek
            filtr = getFiltry().get(selectedRow);
            getFiltry().remove(filtr);
            getFiltry().add(selectedRow-1,filtr);
            getTm().fireTableDataChanged();
            jTable1.setRowSelectionInterval(selectedRow-1, selectedRow-1);
        }
    }//GEN-LAST:event_jButtonMoveUpActionPerformed
    
    private void jButtonSmazActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonSmazActionPerformed
        if(jTable1.getSelectedRow()!=-1) {                            //musi byt vybran nejaky radek
            getFiltry().remove(jTable1.getSelectedRow());
            getTm().fireTableDataChanged();
        }
    }//GEN-LAST:event_jButtonSmazActionPerformed
    
    private void jButtonUpravActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonUpravActionPerformed
        upravaFiltru();
    }//GEN-LAST:event_jButtonUpravActionPerformed
    
    private void jButtonNovyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonNovyActionPerformed
        final JDialog parent = this;
        
        filtr = new Filtr("1==1");
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new FiltrDialog(parent, true, getFiltry(), filtr, true).setVisible(true);
                getTm().fireTableDataChanged();
            }
        });
    }//GEN-LAST:event_jButtonNovyActionPerformed
    
    private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable1MouseClicked
        if(evt.getClickCount()==2){
            upravaFiltru();
        }
    }//GEN-LAST:event_jTable1MouseClicked
    
    /** Closes the dialog */
    private void closeDialog(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_closeDialog
        setVisible(false);
        dispose();
    }//GEN-LAST:event_closeDialog
    
    public Filtry getFiltry() {
        return filtry;
    }
    
    public void setFiltry(Filtry filtry) {
        this.filtry = filtry;
    }
    
    public FiltryTableModel getTm() {
        return tm;
    }
    
    public void setTm(FiltryTableModel tm) {
        this.tm = tm;
    }
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonMoveDown;
    private javax.swing.JButton jButtonMoveUp;
    private javax.swing.JButton jButtonNovy;
    private javax.swing.JButton jButtonSmaz;
    private javax.swing.JButton jButtonUprav;
    private javax.swing.JButton jButtonZrus;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    // End of variables declaration//GEN-END:variables
}
